<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="上报时间" prop="reportingDate">
        <el-date-picker clearable
          v-model="queryParams.reportingDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择上报时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="工种类别" prop="jobCategory">
        <el-input
          v-model="queryParams.jobCategory"
          placeholder="请输入工种类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单ID" prop="workorderId">
        <el-input
          v-model="queryParams.workorderId"
          placeholder="请输入工单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作单号" prop="workorderCode">
        <el-input
          v-model="queryParams.workorderCode"
          placeholder="请输入工作单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单名称" prop="workorderName">
        <el-input
          v-model="queryParams.workorderName"
          placeholder="请输入工单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品ID" prop="itemId">
        <el-input
          v-model="queryParams.itemId"
          placeholder="请输入产品ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入产品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="动作" prop="action">
        <el-input
          v-model="queryParams.action"
          placeholder="请输入动作"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="尾后" prop="afterward">
        <el-input
          v-model="queryParams.afterward"
          placeholder="请输入尾后"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工种" prop="jobType">
        <el-select v-model="queryParams.jobType" placeholder="请选择工种" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="单价" prop="unitPrice">
        <el-input
          v-model="queryParams.unitPrice"
          placeholder="请输入单价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计件工资" prop="pieceRateWage">
        <el-input
          v-model="queryParams.pieceRateWage"
          placeholder="请输入计件工资"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计时" prop="times">
        <el-input
          v-model="queryParams.times"
          placeholder="请输入计时"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报废" prop="scrap">
        <el-input
          v-model="queryParams.scrap"
          placeholder="请输入报废"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input
          v-model="queryParams.remarks"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="袋宽" prop="bagWidth">
        <el-input
          v-model="queryParams.bagWidth"
          placeholder="请输入袋宽"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fb:flow:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fb:flow:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fb:flow:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flowList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="记录ID" align="center" prop="recordId" />
      <el-table-column label="上报时间" align="center" prop="reportingDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工种类别" align="center" prop="jobCategory" />
      <el-table-column label="姓名" align="center" prop="username" />
      <el-table-column label="工单ID" align="center" prop="workorderId" />
      <el-table-column label="工作单号" align="center" prop="workorderCode" />
      <el-table-column label="工单名称" align="center" prop="workorderName" />
      <el-table-column label="产品ID" align="center" prop="itemId" />
      <el-table-column label="产品编码" align="center" prop="itemCode" />
      <el-table-column label="产品名称" align="center" prop="itemName" />
      <el-table-column label="数量" align="center" prop="number" />
      <el-table-column label="动作" align="center" prop="action" />
      <el-table-column label="尾后" align="center" prop="afterward" />
      <el-table-column label="工种" align="center" prop="jobType" />
      <el-table-column label="单价" align="center" prop="unitPrice" />
      <el-table-column label="计件工资" align="center" prop="pieceRateWage" />
      <el-table-column label="计时" align="center" prop="times" />
      <el-table-column label="报废" align="center" prop="scrap" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="袋宽" align="center" prop="bagWidth" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fb:flow:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fb:flow:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改后道流水对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="上报时间" prop="reportingDate">
          <el-date-picker clearable
            v-model="form.reportingDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上报时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工种类别" prop="jobCategory">
          <el-input v-model="form.jobCategory" placeholder="请输入工种类别" />
        </el-form-item>
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="工单ID" prop="workorderId">
          <el-input v-model="form.workorderId" placeholder="请输入工单ID" />
        </el-form-item>
        <el-form-item label="工作单号" prop="workorderCode">
          <el-input v-model="form.workorderCode" placeholder="请输入工作单号" />
        </el-form-item>
        <el-form-item label="工单名称" prop="workorderName">
          <el-input v-model="form.workorderName" placeholder="请输入工单名称" />
        </el-form-item>
        <el-form-item label="产品ID" prop="itemId">
          <el-input v-model="form.itemId" placeholder="请输入产品ID" />
        </el-form-item>
        <el-form-item label="产品编码" prop="itemCode">
          <el-input v-model="form.itemCode" placeholder="请输入产品编码" />
        </el-form-item>
        <el-form-item label="产品名称" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="数量" prop="number">
          <el-input v-model="form.number" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="动作" prop="action">
          <el-input v-model="form.action" placeholder="请输入动作" />
        </el-form-item>
        <el-form-item label="尾后" prop="afterward">
          <el-input v-model="form.afterward" placeholder="请输入尾后" />
        </el-form-item>
        <el-form-item label="工种" prop="jobType">
          <el-select v-model="form.jobType" placeholder="请选择工种">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="单价" prop="unitPrice">
          <el-input v-model="form.unitPrice" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="计件工资" prop="pieceRateWage">
          <el-input v-model="form.pieceRateWage" placeholder="请输入计件工资" />
        </el-form-item>
        <el-form-item label="计时" prop="times">
          <el-input v-model="form.times" placeholder="请输入计时" />
        </el-form-item>
        <el-form-item label="报废" prop="scrap">
          <el-input v-model="form.scrap" placeholder="请输入报废" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="袋宽" prop="bagWidth">
          <el-input v-model="form.bagWidth" placeholder="请输入袋宽" />
        </el-form-item>
        <el-form-item label="数据行状态(0:正常/1:删除)" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入数据行状态(0:正常/1:删除)" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { listFlow, getFlow, delFlow, addFlow, updateFlow } from "@/api/fb/flow";

export default {
  name: "Flow",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 后道流水表格数据
      flowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reportingDate: null,        jobCategory: null,        username: null,        workorderId: null,        workorderCode: null,        workorderName: null,        itemId: null,        itemCode: null,        itemName: null,        number: null,        action: null,        afterward: null,        jobType: null,        unitPrice: null,        pieceRateWage: null,        times: null,        scrap: null,        remarks: null,        bagWidth: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        number: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],      }
    };
  },
  created() {
    // this.getList();
  },
  methods: {
    /** 查询后道流水列表 */
    getList() {
      this.loading = true;
      listFlow(this.queryParams).then(response => {
        this.flowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,        reportingDate: null,        jobCategory: null,        username: null,        workorderId: null,        workorderCode: null,        workorderName: null,        itemId: null,        itemCode: null,        itemName: null,        number: null,        action: null,        afterward: null,        jobType: null,        unitPrice: null,        createBy: null,        pieceRateWage: null,        times: null,        scrap: null,        remarks: null,        bagWidth: null,        createTime: null,        updateBy: null,        updateTime: null,        delFlag: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加后道流水";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids
      getFlow(recordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改后道流水";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recordId != null) {
            updateFlow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFlow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids;
      this.$modal.confirm('是否确认删除后道流水编号为"' + recordIds + '"的数据项？').then(function() {
        return delFlow(recordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fb/flow/export', {
        ...this.queryParams
      }, `flow_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
